package gui.settings;

import gui.MainFrame;

public class SettingsPanel extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	
	private MainFrame mainFrame;
	
    public SettingsPanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
    	initComponents();
    }

    private void initComponents() {

        buttonGroupStemming = new javax.swing.ButtonGroup();
        buttonGroupSynonym = new javax.swing.ButtonGroup();
        buttonGroupOutputChart = new javax.swing.ButtonGroup();
        buttonGroupCompareWith = new javax.swing.ButtonGroup();
        jSliderTermNum = new javax.swing.JSlider();
        jLabelTitle = new javax.swing.JLabel();
        jSliderMinTermFre = new javax.swing.JSlider();
        jLabelSynonym = new javax.swing.JLabel();
        jLabelStemming = new javax.swing.JLabel();
        jLabelOutputChart = new javax.swing.JLabel();
        jLabelCompareWith = new javax.swing.JLabel();
        jRadioButtonStemTrue = new javax.swing.JRadioButton();
        jRadioButtonStemFalse = new javax.swing.JRadioButton();
        jRadioButtonSynFalse = new javax.swing.JRadioButton();
        jRadioButtonSynTrue = new javax.swing.JRadioButton();
        jRadioButton1stChart = new javax.swing.JRadioButton();
        jRadioButton2ndChart = new javax.swing.JRadioButton();        
        jRadioButtonRemoteFile = new javax.swing.JRadioButton();
        jRadioButtonLocalFile = new javax.swing.JRadioButton();
        jButtonSearchAndDownload = new javax.swing.JButton();


        setBackground(new java.awt.Color(255, 255, 255));

        jSliderTermNum.setBackground(new java.awt.Color(255, 255, 255));
        jSliderTermNum.setMajorTickSpacing(5);
        jSliderTermNum.setMaximum(20);
        jSliderTermNum.setMinorTickSpacing(1);
        jSliderTermNum.setPaintLabels(true);
        jSliderTermNum.setPaintTicks(true);
        jSliderTermNum.setValue(ConfigFile.getInstance().getTERM_NUM());
        jSliderTermNum.setBorder(javax.swing.BorderFactory.createTitledBorder("Terms number"));
        
        jSliderTermNum.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderTermNumStateChanged(evt);
            }
        });
        jLabelTitle.setFont(new java.awt.Font("Tahoma", 3, 14));
        jLabelTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelTitle.setText("Settings");

        jSliderMinTermFre.setBackground(new java.awt.Color(255, 255, 255));
        jSliderMinTermFre.setMajorTickSpacing(10);
        jSliderMinTermFre.setMaximum(50);
        jSliderMinTermFre.setMinorTickSpacing(1);
        jSliderMinTermFre.setPaintLabels(true);
        jSliderMinTermFre.setPaintTicks(true);
        jSliderMinTermFre.setValue(ConfigFile.getInstance().getTERM_MIN_FREQ());
        jSliderMinTermFre.setBorder(javax.swing.BorderFactory.createTitledBorder("Minimum term's frequency"));
        
        jSliderMinTermFre.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderMinTermFreStateChanged(evt);
            }
        });

        jLabelStemming.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelStemming.setText("Use stemming ?");

        jRadioButtonStemTrue.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupStemming.add(jRadioButtonStemTrue);
        jRadioButtonStemTrue.setText("Yes");
        jRadioButtonStemTrue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonStemTrueActionPerformed(evt);
            }
        });

        jRadioButtonStemFalse.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupStemming.add(jRadioButtonStemFalse);
        jRadioButtonStemFalse.setText("No");
        jRadioButtonStemFalse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButtonStemFalseActionPerformed(evt);
            }
        });
        

        if(ConfigFile.getInstance().isSTEMMING_RESULTS()) 
        	jRadioButtonStemTrue.setSelected(true);
        else
        	jRadioButtonStemFalse.setSelected(true);

        jRadioButtonSynFalse.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupSynonym.add(jRadioButtonSynFalse);
        jRadioButtonSynFalse.setText("No");
        jRadioButtonSynFalse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButtonSynFalseActionPerformed(evt);
            }
        });

        jRadioButtonSynTrue.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupSynonym.add(jRadioButtonSynTrue);
        jRadioButtonSynTrue.setText("Yes");
        jRadioButtonSynTrue.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButtonSynTrueActionPerformed(evt);
            }
        });
        
        if(ConfigFile.getInstance().isADD_SYNONYMS()) 
        	jRadioButtonSynTrue.setSelected(true);
        else
        	jRadioButtonSynFalse.setSelected(true);

        jLabelSynonym.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelSynonym.setText("Use synonym frequency ?");
        
        jRadioButton1stChart.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupOutputChart.add(jRadioButton1stChart);
        jRadioButton1stChart.setText("1st");
        jRadioButton1stChart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButton1stChartActionPerformed(evt);
            }
        });

        jRadioButton2ndChart.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupOutputChart.add(jRadioButton2ndChart);
        jRadioButton2ndChart.setText("2nd");
        jRadioButton2ndChart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButton2ndChartActionPerformed(evt);
            }
        });
        
        jRadioButton1stChart.setSelected(true);
        
        jLabelOutputChart.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelOutputChart.setText("Select output chart");


        jRadioButtonRemoteFile.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupCompareWith.add(jRadioButtonRemoteFile);
        jRadioButtonRemoteFile.setText("Remote file");
        jRadioButtonRemoteFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButtonRemoteFileActionPerformed(evt);
            }
        });

        jRadioButtonLocalFile.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroupCompareWith.add(jRadioButtonLocalFile);
        jRadioButtonLocalFile.setText("Local file");
        jRadioButtonLocalFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jRadioButtonLocalFileActionPerformed(evt);
            }
        });

        jLabelCompareWith.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelCompareWith.setText("Compare with");

        jButtonSearchAndDownload.setText("Search & Download");
        jButtonSearchAndDownload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSearchAndDownloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(jSliderTermNum, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
                            .addComponent(jSliderMinTermFre, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jRadioButtonStemFalse, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jRadioButtonStemTrue, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jRadioButtonSynFalse, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jRadioButtonSynTrue, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelSynonym, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jRadioButtonRemoteFile, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jRadioButtonLocalFile, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelOutputChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                                    .addComponent(jLabelCompareWith, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                                    .addComponent(jRadioButton1stChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jRadioButton2ndChart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButtonSearchAndDownload, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addComponent(jLabelStemming, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSliderTermNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSliderMinTermFre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabelStemming)
                .addGap(11, 11, 11)
                .addComponent(jRadioButtonStemTrue)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonStemFalse)
                .addGap(13, 13, 13)
                .addComponent(jLabelSynonym)
                .addGap(11, 11, 11)
                .addComponent(jRadioButtonSynTrue)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonSynFalse)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelCompareWith, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButtonLocalFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButtonRemoteFile)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabelOutputChart, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton1stChart)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2ndChart)
                .addGap(18, 18, 18)
                .addComponent(jButtonSearchAndDownload)
                .addContainerGap(33, Short.MAX_VALUE))
        );
        
    }

    private void jSliderTermNumStateChanged(javax.swing.event.ChangeEvent evt) {                                            
		ConfigFile.getInstance().setTERM_NUM(jSliderTermNum.getValue());
		updateChart();
    }                                           

    private void jSliderMinTermFreStateChanged(javax.swing.event.ChangeEvent evt) {                                               
    	ConfigFile.getInstance().setTERM_MIN_FREQ(jSliderMinTermFre.getValue());
    	updateChart();  	
    }  
	
	private void jRadioButtonStemTrueActionPerformed(java.awt.event.ActionEvent evt) {
		ConfigFile.getInstance().setSTEMMING_RESULTS(true);
		updateIndexAndChart();
	}
	
	private void jRadioButtonStemFalseActionPerformed(java.awt.event.ActionEvent evt) {
		ConfigFile.getInstance().setSTEMMING_RESULTS(false);
		updateIndexAndChart();
	}
	
	private void jRadioButtonSynTrueActionPerformed(java.awt.event.ActionEvent evt) {
		ConfigFile.getInstance().setADD_SYNONYMS(true);
		updateIndexAndChart();
	}
	
	private void jRadioButtonSynFalseActionPerformed(java.awt.event.ActionEvent evt) {
		ConfigFile.getInstance().setADD_SYNONYMS(false);
		updateIndexAndChart();
		mainFrame.resetTextArea();
	}
	
	private void jRadioButton1stChartActionPerformed(java.awt.event.ActionEvent evt) {
	
	}
	
	private void jRadioButton2ndChartActionPerformed(java.awt.event.ActionEvent evt) {
		
	}
	
	private void jRadioButtonRemoteFileActionPerformed(java.awt.event.ActionEvent evt) {
		jRadioButton1stChart.setEnabled(false);
		jRadioButton2ndChart.setEnabled(false);
		jButtonSearchAndDownload.setEnabled(true);
		mainFrame.empty2ndChart();
	}

	private void jRadioButtonLocalFileActionPerformed(java.awt.event.ActionEvent evt) {
		jRadioButton1stChart.setEnabled(true);
		jRadioButton2ndChart.setEnabled(true);
		jRadioButton2ndChart.setSelected(true);
		jButtonSearchAndDownload.setEnabled(false);
		
	}
	
	private void jButtonSearchAndDownloadActionPerformed(java.awt.event.ActionEvent evt) {
		mainFrame.searchAndDownload();
	}
	
	public void updateIndexAndChart(){
		mainFrame.updateIndex();
		updateChart();
	}
	
	public void updateChart(){
		mainFrame.updateChart();
		ConfigFile.getInstance().save();
	}
	
	public void setNotEnabled(){       
		jSliderMinTermFre.setEnabled(false);
	    jSliderTermNum.setEnabled(false);
		jRadioButtonStemFalse.setEnabled(false);
	    jRadioButtonStemTrue.setEnabled(false);
		jRadioButtonSynFalse.setEnabled(false);
        jRadioButtonSynTrue.setEnabled(false);
        jRadioButton1stChart.setEnabled(false);
        jRadioButton2ndChart.setEnabled(false);
        jRadioButtonRemoteFile.setEnabled(false);
        jRadioButtonLocalFile.setEnabled(false);
        jButtonSearchAndDownload.setEnabled(false);
	}
	
	public void setEnabled(){       
		jSliderMinTermFre.setEnabled(true);
	    jSliderTermNum.setEnabled(true);
		jRadioButtonStemFalse.setEnabled(true);
	    jRadioButtonStemTrue.setEnabled(true);
		jRadioButtonSynFalse.setEnabled(true);
        jRadioButtonSynTrue.setEnabled(true);
        jRadioButtonLocalFile.setEnabled(true);
        jRadioButtonRemoteFile.setEnabled(true);
	}
	
	private javax.swing.ButtonGroup buttonGroupCompareWith;
	private javax.swing.ButtonGroup buttonGroupStemming;
    private javax.swing.ButtonGroup buttonGroupSynonym;
    private javax.swing.ButtonGroup buttonGroupOutputChart;
    private javax.swing.JButton jButtonSearchAndDownload;
    private javax.swing.JLabel jLabelCompareWith;
    private javax.swing.JLabel jLabelStemming;
    private javax.swing.JLabel jLabelSynonym;
    private javax.swing.JLabel jLabelOutputChart;
    private javax.swing.JLabel jLabelTitle;
    public javax.swing.JRadioButton jRadioButtonStemFalse;
    public javax.swing.JRadioButton jRadioButtonStemTrue;
    public javax.swing.JRadioButton jRadioButtonSynFalse;
    public javax.swing.JRadioButton jRadioButtonSynTrue;
    public javax.swing.JRadioButton jRadioButtonLocalFile;
    public javax.swing.JRadioButton jRadioButtonRemoteFile;
    public javax.swing.JRadioButton jRadioButton1stChart;
    public javax.swing.JRadioButton jRadioButton2ndChart;
    public javax.swing.JSlider jSliderMinTermFre;
    public javax.swing.JSlider jSliderTermNum;

}
